package gson.emlog.adapter;

import gson.emlog.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LinkAdapter extends BaseAdapter {
	private JSONArray items;
	private Context mContext;
	
	public LinkAdapter(JSONArray items, Context mContext) {
		super();
		this.items = items;
		this.mContext = mContext;
	}

	public int getCount() {
		return items.length();
	}

	public JSONObject getItem(int position) {
		try {
			return items.getJSONObject(position);
		} catch (JSONException e) {
			return null;
		}
	}

	public long getItemId(int position) {
		try {
			JSONObject obj = this.getItem(position);
			if (obj != null) {
				return obj.getInt("id");
			}
		} catch (JSONException e) {
			return position;
		}
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		//获取layout实例对象
		LayoutInflater lif = LayoutInflater.from(mContext);
		LinearLayout item = (LinearLayout) lif.inflate(R.layout.simpitemtext, null);
		if(item != null){
			try {
				JSONObject data = items.getJSONObject(position);
				//赋值
				TextView  textName = (TextView) item.findViewById(R.id.sptext);
				textName.setText(data.getString("stn"));
				
				return item;
			} catch (JSONException e) {
				return convertView;
			}
		}
		return convertView;
	}

}
